Research
Security News
Malicious PyPI Package ‘pycord-self’ Targets Discord Developers with Token Theft and Backdoor Exploit
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
fetch-ponyfill
Advanced tools
A ponyfill (doesn't overwrite the native fetch) for the Fetch specification https://fetch.spec.whatwg.org.
The fetch-ponyfill npm package provides a ponyfill for the Fetch API, which means it offers a way to use the Fetch API in environments where it is not natively available, such as older browsers or Node.js. Unlike a polyfill, a ponyfill does not modify the global environment but instead provides the functionality as a module.
Basic Fetch Request
This code demonstrates how to perform a basic fetch request using fetch-ponyfill. It fetches data from a placeholder API and logs the response.
const fetch = require('fetch-ponyfill')().fetch;
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Custom Headers
This code demonstrates how to make a POST request with custom headers using fetch-ponyfill. It sends a JSON payload to the server and logs the response.
const fetch = require('fetch-ponyfill')().fetch;
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token'
},
body: JSON.stringify({ title: 'foo', body: 'bar', userId: 1 })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Handling Errors
This code demonstrates how to handle errors in fetch requests using fetch-ponyfill. It checks if the response is not ok and throws an error if the fetch fails.
const fetch = require('fetch-ponyfill')().fetch;
fetch('https://jsonplaceholder.typicode.com/invalid-url')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.json();
})
.then(data => console.log(data))
.catch(error => console.error('Fetch error:', error));
node-fetch is a lightweight module that brings window.fetch to Node.js. It is similar to fetch-ponyfill in that it provides Fetch API functionality in environments where it is not natively available. However, node-fetch is specifically designed for Node.js and does not work in the browser.
axios is a promise-based HTTP client for the browser and Node.js. It provides a more feature-rich API compared to fetch-ponyfill, including support for request and response interceptors, automatic JSON transformation, and more. Unlike fetch-ponyfill, axios is not a ponyfill for the Fetch API but a separate library with its own API.
isomorphic-fetch is a library that allows you to use the Fetch API in both Node.js and browser environments. It is similar to fetch-ponyfill in that it aims to provide a consistent Fetch API across different environments. However, isomorphic-fetch is a polyfill and modifies the global environment, unlike fetch-ponyfill which is a ponyfill.
WHATWG
fetch
ponyfill
This module wraps the github/fetch polyfill in a CommonJS module
for browserification, and avoids appending anything to the window, instead returning a setup
function when fetch-ponyfill
is required. Inspired by
object-assign.
When used in Node, delegates to node-fetch
instead.
const {fetch, Request, Response, Headers} = require('fetch-ponyfill')(options);
where options is an object with the following optional properties:
option | description |
---|---|
Promise | An A+ Promise implementation. Defaults to window.Promise in the browser, and global.Promise in Node. |
XMLHttpRequest | The XMLHttpRequest constructor. This is useful to feed in when working with Firefox OS. Defaults to window.XMLHttpRequest . Has no effect in Node. |
FAQs
A ponyfill (doesn't overwrite the native fetch) for the Fetch specification https://fetch.spec.whatwg.org.
We found that fetch-ponyfill demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.